import { postJson } from './request';

interface MessageOptions {
    level: 'debug' | 'info' | 'success' | 'warning' | 'error';
    message: string;
}

export class DjangoMessageService {
    private messageEndpoint: string;
  
    constructor(messageEndpoint: string = '/api/message/add') {
        this.messageEndpoint = messageEndpoint;
    }
  
    /**
     * Add a message to Django's message framework
     * @param options Message options including level, message text and display preferences
     * @returns Promise resolving to boolean indicating success
     */
    async addMessage(options: MessageOptions): Promise<boolean> {
        const { level, message } = options;
    
        try {
            const response = await postJson(this.messageEndpoint, { 
                level, 
                message 
            }, {
                // We'll handle errors ourselves
                alertError: false
            });
      
            if (response.ok) return true;
      
            console.error('Failed to add Django message:', await response.text());
            return false;
        } catch (error) {
            console.error('Error adding Django message:', error);
            return false;
        }
    }
}
